package com.hlingsoft.cms.action;


import com.hlingsoft.cms.dao.FocusDao;
import com.hlingsoft.cms.dao.FocusMessageDao;
import com.hlingsoft.cms.dao.MessageStatusDao;
import com.hlingsoft.cms.entity.Focus;
import com.hlingsoft.cms.entity.FocusMessage;
import com.hlingsoft.cms.entity.MessageStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * Created by selbylei on 2015/5/12.
 */

@Controller
@RequestMapping("/focus")
public class FocusAction extends BaseAction {

    @Autowired
    private FocusDao focusDao;

    @Autowired
    private FocusMessageDao focusMessageDao;

    @Autowired
    private MessageStatusDao messageStatusDao;

    /**
     * 添加关注
     *
     * @param myId
     * @param toId
     * @param request
     * @return
     */
    @RequestMapping(value = "/add.json")
    @ResponseBody
    public Map<String, Object> add(
            @RequestParam(value = "myId") long myId,
            @RequestParam(value = "toId") long toId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Date now = new Date();
            if (myId != toId) {
                Focus checkFocus = focusDao.getFocusByIdsWithNone(myId, toId);
                //首次关注
                if (checkFocus == null) {
                    Focus focus = new Focus();
                    focus.setMyId(myId);
                    focus.setToId(toId);
                    focus.setCreateTime(now);
                    focus.setUpdateTime(now);
                    focusDao.addFocus(focus);


                    FocusMessage checkFocusMessage = focusMessageDao.getFocusMessageByIds(myId, toId);
                    if (checkFocusMessage != null) {
                        checkFocusMessage.setUpdateTime(now);
                        checkFocusMessage.setAuthorization(1);
                        focusMessageDao.updateFocusMessage(checkFocusMessage);
                    } else {
                        FocusMessage focusMessage = new FocusMessage();
                        focusMessage.setMyId(myId);
                        focusMessage.setToId(toId);
                        focusMessage.setCreateTime(now);
                        focusMessage.setUpdateTime(now);
                        if (focusDao.getFocusByIds(toId, myId) == null) {
                            focusMessage.setIsFocus(0);
                        } else {
                            focusMessage.setIsFocus(1);
                        }
                        focusMessageDao.addFocusMessage(focusMessage);

                        MessageStatus messageStatus = new MessageStatus();
                        messageStatus.setAppUserId(focus.getToId());
                        messageStatus.setMessageId(focusMessage.getId());
                        messageStatus.setMessageType(3);
                        messageStatusDao.addMessageStatus(messageStatus);
                    }


                } else {
                    checkFocus.setAuthorization(1);
                    focusDao.updateFocus(checkFocus);
                    //之前是否有消息
                    FocusMessage checkFocusMessage = focusMessageDao.getFocusMessageByIds(myId, toId);
                    if (checkFocusMessage != null) {
                        checkFocusMessage.setUpdateTime(now);
                        checkFocusMessage.setAuthorization(1);
                        focusMessageDao.updateFocusMessage(checkFocusMessage);
                    } else {
                        FocusMessage focusMessage = new FocusMessage();
                        focusMessage.setMyId(myId);
                        focusMessage.setToId(toId);
                        focusMessage.setCreateTime(now);
                        focusMessage.setUpdateTime(now);
                        if (focusDao.getFocusByIds(toId, myId) == null) {
                            focusMessage.setIsFocus(0);
                        } else {
                            focusMessage.setIsFocus(1);
                        }
                        focusMessageDao.addFocusMessage(focusMessage);
                    }

                }

            } else {
                resultMap.put("msg", "自己不能关注自己");
            }
            resultMap.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


    /**
     * 取消关注
     *
     * @param myId
     * @param toId
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete.json")
    @ResponseBody
    public Map<String, Object> delete(
            @RequestParam(value = "myId") long myId,
            @RequestParam(value = "toId") long toId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Focus focus = focusDao.getFocusByIds(myId, toId);

            if (focus != null) {
                focus.setAuthorization(0);
                focusDao.updateFocus(focus); //删除关注

                FocusMessage focusMessage = focusMessageDao.getFocusMessageByIds(myId, toId);
                focusMessage.setUpdateTime(new Date());
                focusMessage.setAuthorization(0);
                focusMessageDao.updateFocusMessage(focusMessage);

            } else {
                resultMap.put("result", "您并未关注此用户");
            }
            resultMap.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


}
